package com.rent.spider;

import org.apache.log4j.Logger;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

/*
@李子宁
Happy,happy everyday!
冲鸭！
2020/8/8

*/
public class Thread5i5j extends Thread {
    String string;
    Document doc;


    Thread5i5j(String str, Document docc) {
        string = str;
        doc = docc;
    }

    private static Logger logger = Logger.getLogger(Thread5i5j.class);

    public static void main(String[] args) throws IOException {
        String proxyHost = "127.0.0.1";
        String proxyPort = "11478";
        System.setProperty("http.proxyHost", proxyHost);
        System.setProperty("http.proxyPort", proxyPort);
        // 对https也开启代理
        System.setProperty("https.proxyHost", proxyHost);
        System.setProperty("https.proxyPort", proxyPort);  //设置代理来访问豆瓣

        String link = "https://nj.5i5j.com/zufang/n2/";
        Connection connect = Jsoup.connect(link);
        Document doc = connect.get();
        Elements elements = doc.getElementsByClass("pList");
        for (Element element : elements) {
            Elements li = element.getElementsByTag("li");
            for (Element ele : li) {
                String info = Html2Text.getContent(ele.getElementsByClass("jia").html());
                System.out.println(info);
            }
        }


    }

    synchronized public void run() {
        try {
            Elements elements = doc.getElementsByClass("pList");
            for (Element element : elements) {
                Elements li = element.getElementsByTag("li");
                for (Element ele : li) {
                    try {
                    Thread.sleep(2000);
                    //获取房子的图片链接
                    String imgUrl = ele.getElementsByClass("lazy").attr("src") + ele.getElementsByClass("lazy").attr("data-src");
                    //获取房子的具体信息链接
                    String houseSingleUrl ="https://nj.5i5j.com" + ele.getElementsByClass("listImg").get(0).getElementsByTag("a").attr("href");
                    //房子的标题
                    String title = ele.getElementsByClass("listImg").get(0).getElementsByTag("img").attr("title");

                    String replace = Html2Text.getContent(ele.getElementsByClass("listX").get(0).html());
                    String[] info = replace.split(" · ");
                    //获取房型：1室2厅
                    String singleType = "";
                    //获取面积
                    String size = "";
                    //获取精装，简装
                    String direcType = "";
                    //获取楼层
                    String singleLayer = "";
                    //整租，合租，出租类型
                    String rentType = "";
                    //获取房型
                    singleType = info[0];
                    //获取面积
                    size = info[1];
                    //获取楼层
                    direcType = info[4];
                    //获取出租类型
                    singleLayer = info[3];
                    rentType = info[info.length - 1].split("：")[1];

                    //秦淮-月牙湖-天坛新村^空房
                    Thread.sleep(2000);
                    String place = info[4];
                    //南京市月牙湖小学
                    String underPlace = "";

                    underPlace = info[5];

                    //房子的价格
                    String price = Html2Text.getContent(ele.getElementsByClass("redC").html());
                    //输出信息到日志
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.append(imgUrl.replace("\t", "").replace("\r", "").replace("\n", "")).append("^")
                            .append(houseSingleUrl.replace("\t", "").replace("\r", "").replace("\n", "")).append("^")
                            .append(title.replace("\t", "").replace("\r", "").replace("\n", "")).append("^")
                            .append(singleType.replace("\t", "").replace("\r", "").replace("\n", "")).append("^")
                            .append(size.replace("\t", "").replace("\r", "").replace("\n", "")).append("^")
                            .append(direcType.replace("\t", "").replace("\r", "").replace("\n", "")).append("^")
                            .append(singleLayer.replace("\t", "").replace("\r", "").replace("\n", "")).append("^")
                            .append(rentType.replace("\t", "").replace("\r", "").replace("\n", "")).append("^")
                            .append(place.replace("\t", "").replace("\r", "").replace("\n", "")).append("^")
                            .append(underPlace.replace("\t", "").replace("\r", "").replace("\n", "")).append("^")
                            .append(price.replace("\t", "").replace("\r", "").replace("\n", ""));
                    logger.info(stringBuilder.toString());
                    }catch (IndexOutOfBoundsException e){
                       e.printStackTrace();
                        continue;
                    }
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
